package com.logic.modular.game.player.match.service.base;


import com.alibaba.fastjson2.JSONObject;

import com.logic.comment.util.RedisUtils;
import com.logic.config.netty.web.service.MessageService;
import com.logic.modular.game.core.service.MatchServiceImpl;
import com.logic.modular.game.PvpCity;
import com.comment.pojo.domain.logic.core.PvpMatch;
import com.comment.pojo.domain.logic.core.CoreCity;
import com.logic.modular.gameCore.map.service.CoreCityServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 城池信息基础服务
 */
@Slf4j
@Service
public class PvpCityBaseServiceImpl {
    @Resource
    private RedisTemplate<Object, Object> redisTemplate;
    @Resource
    private HashOperations<Object, Object, PvpCity> hashOperations;
    @Resource
    private CoreCityServiceImpl coreCityServiceImpl;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private MatchServiceImpl pvpMatchServiceImpl;
    public static final String PVP_CITY_CACHE_KEY = "pvpMatch:%d:city";


    /**
     * 更新
     */
    public PvpCity update(PvpCity pvpCity) {
        savaCache(pvpCity);
        JSONObject jsonObject = MessageService.updateJsonObject("pvpCity", pvpCity);
        for (Long playerId : pvpMatchServiceImpl.getById(pvpCity.getMatchId()).playerIds) {
            MessageService.sendMessageToUser(playerId, jsonObject);
        }
        return pvpCity;
    }


    /**
     * 初始化城池信息
     */
    public List<PvpCity> init(PvpMatch pvpMatch) {
        Long pvpMatchId = pvpMatch.getId();
        List<CoreCity> list = coreCityServiceImpl.listCache();
        List<PvpCity> pvpCities = new ArrayList<>();
        list.forEach(coreCity -> {
            PvpCity build = PvpCity.builder()
                    .matchId(pvpMatchId)
                    .lastOccupyTime(new Date())
                    .ownerUid(0L)
                    .coreCityId(coreCity.getId())
                    .type(coreCity.getType())
                    .status(0)
                    // 初始化生产力
                    .grainRate(coreCity.getGrainRate())
                    .copperRate(coreCity.getCopperRate())
                    .readyArmyRate(coreCity.getReadyArmyRate())
                    .readyArmy(coreCity.getDefArmy().longValue())
                    //
                    .build();
            savaCache(build);
            pvpCities.add(build);
        });
        return pvpCities;
    }

    public void savaCache(PvpCity pvpCity) {
        String key1 = String.format(PVP_CITY_CACHE_KEY, pvpCity.getMatchId());
        String key2 = String.valueOf(pvpCity.getCoreCityId());
        hashOperations.put(key1, key2, pvpCity);
        redisTemplate.expire(key1, 300, TimeUnit.MINUTES);
    }

    public PvpCity getCache(Long matchId, Long coreCityId) {
        String key1 = String.format(PVP_CITY_CACHE_KEY, matchId);
        String key2 = String.valueOf(coreCityId);
        return hashOperations.get(key1, key2);
    }

    public List<PvpCity> listCache(Long matchId) {
        String key1 = String.format(PVP_CITY_CACHE_KEY, matchId);
        Collection<PvpCity> values = hashOperations.entries(key1).values();
        return new ArrayList<>(values);
    }

    public void removeCache(Long matchId) {
        String key1 = String.format(PVP_CITY_CACHE_KEY, matchId);
        redisTemplate.delete(key1);
    }
}
